package com.school.aftersalemanagement.controller.ui.main;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.KeyEvent;

import com.school.aftersalemanagement.R;
import com.school.aftersalemanagement.controller.ui.register.RegisterActivity;
import com.school.aftersalemanagement.entity.Bike;
import com.school.aftersalemanagement.entity.User;
import com.school.aftersalemanagement.global.BaseActivity;
import com.school.aftersalemanagement.service.UserService;
import com.school.aftersalemanagement.service.impl.UserServiceimpl;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MainActivity extends BaseActivity{
    private String TAG = "MainActivity";
    private UserService userService;
    private User user;
    @BindView(R.id.activity_main)
    DrawerLayout drawerLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        userService = new UserServiceimpl(this,mHandler);
    }

    @Override
    protected void onResume() {
        super.onResume();
        validateUser();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // 创建退出对话框
            android.app.AlertDialog isExit = new android.app.AlertDialog.Builder(this).create();
            // 设置对话框标题
            isExit.setTitle("系统提示");
            // 设置对话框消息
            isExit.setMessage("确定要退出吗");
            // 添加选择按钮并注册监听
            isExit.setButton("确定", listener);
            isExit.setButton2("取消", listener);
            // 显示对话框
            isExit.show();

        }
        return false;
    }

    /**
     * 监听对话框里面的button点击事件
     */
    DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                case android.app.AlertDialog.BUTTON_POSITIVE:// "确认"按钮退出程序
                    finish();
                    break;
            }
        }
    };
    private int GET_USER = 1;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == GET_USER) {
                user = (User) msg.obj;
                ((MenuFragment)getFragmentManager().findFragmentById(R.id.fragment_main_menu)).refresh();
            }
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 200) {
            //处理扫描结果（在界面上显示）
            if (null != data&&data.getExtras()!=null) {
                Bundle bundle = data.getExtras();
                String bikeNum = (String) bundle.get("bikeNum");
                if (!"".equals(bikeNum)) {
                    MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.fragment_main_map);
                    mapFragment.getmHandler().obtainMessage(mapFragment.SHOW_BIKE, bikeNum).sendToTarget();
                } else{
                    showShort("解析二维码失败");
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    public boolean isAvailable(){
        if(user == null){
            startActivity(RegisterActivity.class);      //未登录
            return false;
        }
        return true;
    }

    public void validateUser(){
        userService.validateUser(getDeviceId(),GET_USER);
    }

    public User getUser() {
        return this.user;
    }
}
